﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace WJ.Udp.Tcp.Test
{
    class Program
    {
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        private static UdpClient udpcSend = null;

        static IPEndPoint localIpep = null;

        public static void Main(string[] args)
        {
            localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888); // 本机IP和监听端口号
            udpcSend = new UdpClient(localIpep);

            #region 测试UDP的发送        
            {
                // 测试UDP的发送
                //string msg = null;
                //while (true)
                //{
                //    Console.WriteLine("请输入要发送的内容：");
                //    msg = Console.ReadLine();
                //    SendMessage(msg);
                //}
            }
            #endregion

            #region 测试加签算法
            SignHelper.InitTargetModel();
            #endregion
            Console.ReadLine();
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="obj"></param>
        private static void SendMessage(object obj)
        {
            try
            {
                string message = (string)obj;
                byte[] sendbytes = Encoding.UTF8.GetBytes(message);
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8899); // 发送到的IP地址和端口号
                int bnum = udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
                Console.WriteLine($"一共发送了{bnum}字节数据");
                //udpcSend.Close();
            }
            catch { }
        }
    }
}
